package com.tang.framework.config.pay.wx;

import cn.binarywang.wx.miniapp.api.*;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.EntPayService;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.tang.common.properties.WxConfigProperties;

import com.tang.framework.core.cache.CoolTeaConfig;
import org.redisson.api.RedissonClient;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;


import jakarta.annotation.Resource;

/**
 * @author tang jun liang
 * @Date 2023/6/25 14:14
 */
@SpringBootConfiguration
public class WxConfig {

    @Resource
    private RedissonClient redissonClient;

    @Bean
    @ConditionalOnProperty(prefix = "cool-tea", name = "wx-pay", havingValue = "true")
    public WxPayService wxPayServiceApi() {
        WxConfigProperties wxConfigProperties = CoolTeaConfig.getConfig(WxConfigProperties.class, redissonClient);
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(wxConfigProperties.getAppId());
        payConfig.setMchId(wxConfigProperties.getMchId());
        payConfig.setMchKey(wxConfigProperties.getMchKey());
        payConfig.setKeyPath(wxConfigProperties.getKeyPath());
        WxPayService wxPayService = new WxPayServiceImpl();
        payConfig.setUseSandboxEnv(false);
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

    @Bean
    @ConditionalOnMissingBean
    public WxMaService wxMaService() {
        WxConfigProperties wxConfigProperties = CoolTeaConfig.getConfig(WxConfigProperties.class, redissonClient);
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(wxConfigProperties.getMiniAppId());
        config.setSecret(wxConfigProperties.getMiniAppSecret());
        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(config);
        return service;
    }

    @Bean
    @ConditionalOnMissingBean
    public WxMaLiveService wxMaLiveService(WxMaService wxMaService) {
        return wxMaService.getLiveService();
    }


    @Bean
    @ConditionalOnMissingBean
    public WxMaMediaService wxMaMediaService(WxMaService wxMaService) {
        return wxMaService.getMediaService();
    }


    @Bean
    @ConditionalOnMissingBean
    public WxMaLiveGoodsService wxMaLiveGoodsService(WxMaService wxMaService) {
        return wxMaService.getLiveGoodsService();
    }

    @Bean
    @ConditionalOnMissingBean
    public WxMaSubscribeService wxMaSubscribeService(WxMaService wxMaService){
        return wxMaService.getSubscribeService();
    }

    @Bean
    @ConditionalOnMissingBean
    public WxMaQrcodeService wxMaQrcodeService(WxMaService wxMaService) {
        return wxMaService.getQrcodeService();
    }


    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "cool-tea", name = "wx-pay", havingValue = "true")
    public EntPayService entPayService(WxPayService wxPayService){
        return wxPayService.getEntPayService();
    }
}
